package com.wanji.gateway.processor;

import com.lmax.disruptor.EventHandler;
import com.wanji.gateway.command.BaseDeviceCommand;
import com.wanji.gateway.command.DeviceCommandBuilder;
import com.wanji.gateway.device.DeviceManager;
import com.wanji.gateway.event.DataEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DataEventHandler implements EventHandler<DataEvent> {

    private final DeviceManager deviceManager;

    @Override
    public void onEvent(DataEvent event, long sequence, boolean endOfBatch) {
        processData(event);
    }

    private void processData(DataEvent data) {
        BaseDeviceCommand command = DeviceCommandBuilder.setParamCommand(data.getDeviceId(), "sensorData", data.getData()).build();
        deviceManager.sendCommand(data.getDeviceId(), command);
    }
}
